<?php

namespace addons\kzvote\library;

use EasyWeChat\Factory;
use Psr\Log\AbstractLogger;
use think\Hook;

/**
 * 订阅消息类
 */
class WechatSubscription
{
    protected $wechat = [];
    protected $config = [];
    public function __construct()
    {
        $this->wechat = kzvote_getConfig('wechat');
        $this->config = kzvote_getConfig('wechat_subscription');
        if (!isset($this->wechat['wechat_url']) || !$this->wechat['wechat_url']){
            return true;
        }
        if (!$this->config){
            return true;
        }
    }

    /**
     * 新活动发布提醒
     */
    public static function vote_create($openid, $url ,$vote_name, $time)
    {
        $static = new self();
        if (!isset($static->config['wechat_huodongfabu']) || !$static->config['wechat_huodongfabu']){
            return true;
        }

        $data = [
            'template_id' => $static->config['wechat_huodongfabu'],
            'touser' => $openid,
            'page' => $static->wechat['wechat_url'].'/#/'.$url,
            'data' => [
                'thing6' => [
                    'value' => $vote_name,
                ],
                'character_string10' => [
                    'value' => $time,
                ],
            ],
        ];
        return self::send($data);
    }

    /**
     * 活动资料审核通知
     */
    public static function vote_check($openid, $url, $vote_name, $time, $check)
    {
        $static = new self();
        if (!isset($static->config['wechat_huodongshenhe']) || !$static->config['wechat_huodongshenhe']){
            return true;
        }
        $data = [
            'template_id' => $static->config['wechat_huodongshenhe'],
            'touser' => $openid,
            'page' => $static->wechat['wechat_url'].'/#/'.$url,
            'data' => [
                'thing1' => [
                    'value' => $vote_name,
                ],
                'date2' => [
                    'value' => $time,
                ],
                'phrase3' => [
                    'value' => $check,
                    "color" => "#f00000",
                ],
                'thing4' => [
                    'value' => '点击查看详情',
                ],
            ],
        ];
        return self::send($data);
    }

    /**
     * 报名成功通知
     */
    public static function apply_success($openid, $url, $vote_name, $time, $status)
    {
        $static = new self();
        if (!isset($static->config['wechat_baomingchenggong']) || !$static->config['wechat_baomingchenggong']){
            return true;
        }
        $data = [
            'template_id' => $static->config['wechat_baomingchenggong'],
            'touser' => $openid,
            'page' => $static->wechat['wechat_url'].'/#/'.$url,
            'data' => [
                'thing2' => [
                    'value' => $vote_name,
                ],
                'date4' => [
                    'value' => $time,
                ],
                'phrase8' => [
                    'value' => $status,
                ],
                'date14' => [
                    'value' => date('Y-m-d H:i:s',time()),
                ],
            ],
        ];
        return self::send($data);
    }

    /**
     * 报名结果通知
     */
    public static function apply_check($openid, $url, $vote_name, $person_name, $status)
    {
        $static = new self();
        if (!isset($static->config['wechat_baomingshenhe']) || !$static->config['wechat_baomingshenhe']){
            return true;
        }
        $data = [
            'template_id' => $static->config['wechat_baomingshenhe'],
            'touser' => $openid,
            'page' => $static->wechat['wechat_url'].'/#/'.$url,
            'data' => [
                'thing1' => [
                    'value' => $vote_name,
                ],
                'thing2' => [
                    'value' => $person_name,
                ],
                'phrase3' => [
                    'value' => $status,
                    "color" => "#f00000",
                ],
                'thing4' => [
                    'value' => '点击查看详情',
                ],
            ],
        ];
        return self::send($data);
    }

    /**
     * 投票结果通知
     */
    public static function vote_result($openid, $url, $vote_name, $person_name, $num)
    {
        $static = new self();
        if (!isset($static->config['wechat_toupiaojieguo']) || !$static->config['wechat_toupiaojieguo']){
            return true;
        }
        $data = [
            'template_id' => $static->config['wechat_toupiaojieguo'],
            'touser' => $openid,
            'page' => $static->wechat['wechat_url'].'/#/'.$url,
            'data' => [
                'thing1' => [
                    'value' => $vote_name,
                ],
                'thing12' => [
                    'value' => $person_name,
                ],
                'thing4' => [
                    "value" => '投票成功，作品票数+'.$num,
                    "color" => "#173177",
                ],
                'thing10' => [
                    'value' => '点击查看详情',
                ],
            ],
        ];
        return self::send($data);
    }

    /**
     * 发送订阅消息
     *
     * @param [type] $data 模板内容
     * @return bool
     */
    private static function send($data)
    {
        try {
            $service = new WechatService();
            $app = $service->getApp();
            $accessToken = $app->access_token;
            $token = $accessToken->getToken();
            $result = kzvote_postCurl('https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token='.$token['access_token'],$data);
            //发送不成功
            if ($result->errcode != 0) {
                \think\Log::write(json_encode($result),'error');
                return false;
            }
            return true;
        }catch (\Exception $e){
            return null;
        }
    }

}
